﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using SYcwpb.ViewModels;
using SYcwpb.Helpers;
using System.Data;

namespace SYcwpb.Views
{
    /// <summary>
    /// Nofity.xaml 的交互逻辑
    /// </summary>
    public partial class NofityView : Window
    {
        private NofityViewModel viewModel = null;

        private static NofityView _instance = null;
        public static NofityView Instance
        {
            get
            {
                if (_instance == null || !_instance.IsActive)
                    _instance = new NofityView();
                return _instance;
            }
        }

        public NofityView()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                
                viewModel = new NofityViewModel();
                this.DataContext = viewModel;

                
                
                //this.dtp_DeadLine.DisplayDateStart = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
                //this.dtp_DeadLine.IsEnabled = false;
            }
            catch (Exception ex)
            {
                MessageBoxHelper.ShowError(ex);
            }
        }

        private void btn_exit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void rtb_notify_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                viewModel.NcContent = new TextRange(rtb_notify.Document.ContentStart, rtb_notify.Document.ContentEnd).Text;
                viewModel.NotifyContentChanged();
            }
            catch (Exception ex)
            {
                MessageBoxHelper.ShowError(ex);
            }
        }
        private Run GetRTBRun(Brush foreground, double fontsize, FontFamily fontfamily, FontWeight fontweight, string content)
        {
            Run run = new Run();
            run.Foreground = foreground;
            run.FontSize = fontsize;
            run.FontFamily = fontfamily;
            run.FontWeight = fontweight;
            run.Text = content;

            return run;
        }
        /// <summary>
        /// 根据通知等级设计字体颜色
        /// </summary>
        /// <param name="level">通知等级：2.紧急，1.重要，0.正常</param>
        /// <returns></returns>
        private Brush GetColor(int level)
        {
            if (level == 2)
            {
                return Brushes.Red;
            }
            else if (level == 1)
            {
                return Brushes.Green;
            }
            else
            {
                return Brushes.Black;
            }
        }
        private void cb_notify_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                
                DataRow row = (cb_notify.SelectedValue as DataRowView).Row;
                if (row == null)
                    return;
                string content = viewModel.NotityInit(Convert.ToInt32(row["id"]));
                int type = viewModel.GetNotifyTypeID();
                if (!string.IsNullOrEmpty(content))
                {
                    
                    if (type == 1)
                    {
                        System.IO.StringReader sr = new System.IO.StringReader(content);
                        System.Xml.XmlReader xr = System.Xml.XmlReader.Create(sr);
                        rtb_notify.Document = (FlowDocument)System.Windows.Markup.XamlReader.Load(xr);
                        viewModel.NcContent = new TextRange(rtb_notify.Document.ContentStart, rtb_notify.Document.ContentEnd).Text;
                    }
                    else if(type==3)
                    {
                        rtb_notify.Document = new FlowDocument(new Paragraph(GetRTBRun(GetColor(viewModel.UrgencyLevel), Convert.ToDouble(viewModel.FontSize), new FontFamily("楷体"), FontWeights.Bold, content)));
                    }
                    else
                    {
                        rtb_notify.Document = new FlowDocument(new Paragraph(GetRTBRun(GetColor(viewModel.UrgencyLevel), Convert.ToDouble(viewModel.FontSize), new FontFamily("楷体"), FontWeights.UltraBlack, string.Format("\n{0}\n\n", content))));
                    }

                }
                else
                {
                    if(type==4){
                        rtb_notify.Document = new FlowDocument(new Paragraph(GetRTBRun(GetColor(viewModel.UrgencyLevel), Convert.ToDouble(viewModel.FontSize), new FontFamily("楷体"), FontWeights.UltraBlack, string.Format("\n{0}\n\n", ""))));
                    }else{
                        rtb_notify.Document = new FlowDocument(new Paragraph(GetRTBRun(GetColor(viewModel.UrgencyLevel), Convert.ToDouble(viewModel.FontSize), new FontFamily("楷体"), FontWeights.Bold, "")));
                    }
                    //rtb_notify.Document = new FlowDocument(new Paragraph(GetRTBRun(GetColor(viewModel.UrgencyLevel), Convert.ToDouble(viewModel.FontSize), new FontFamily("楷体"), FontWeights.Bold, "")));
                }
                
                viewModel.EnterWordCount = viewModel .MaxCount - viewModel.NcContent.Length;
            }
            catch (Exception ex)
            {
                MessageBoxHelper.ShowError(ex);
            }
        }

        private void btn_savenofity_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string content = null;
                int type = viewModel.GetNotifyTypeID();
                content = System.Windows.Markup.XamlWriter.Save(rtb_notify.Document);

                TextRange textRange = new TextRange(rtb_notify.Document.ContentStart, rtb_notify.Document.ContentEnd);
                string text = textRange.Text;
                //string xw = System.Windows.Markup.XamlWriter.Save(rtb_notify.Document);
                viewModel.SaveNotify(text,content);
            }
            catch (Exception ex)
            {
                MessageBoxHelper.ShowError(ex);
            }
        }

        private void cb_fontSize_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                FlowDocument doc = rtb_notify.Document;
                if (viewModel == null)
                    return;
                int type = viewModel.GetNotifyTypeID();
                if (type == 1)
                {

                   object obj = (object)e.AddedItems;
                   ComboBoxItem item = ((object[])obj)[0] as ComboBoxItem;

                   rtb_notify.Selection.ApplyPropertyValue(TextElement.FontSizeProperty, item.Content);
                }

            }
            catch (Exception ex)
            {
                MessageBoxHelper.ShowError(ex);
            }
        }

        private void cb_UrgencyLevel_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                FlowDocument doc = rtb_notify.Document;
                if (viewModel == null)
                    return;
                int type = viewModel.GetNotifyTypeID();
                if (type == 1)
                {

                    rtb_notify.Selection.ApplyPropertyValue(TextElement.ForegroundProperty, GetColor(viewModel.UrgencyLevel));
                }
            }
            catch (Exception ex)
            {
                MessageBoxHelper.ShowError(ex);
            }
        }

    }
}
